﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using School.Entities;
using System.Collections;
using System.IO;
using School.Admission.ReportsCls;

namespace School.Admission
{
    public static class StudentAttach
    {
        private static SchoolEntities db = new SchoolEntities();

        public static Int64 Add(SD_Student_Attachments Attachment)
        {
            try
            {
                db.AddToSD_Student_Attachments(Attachment);

                db.SaveChanges();
                return Attachment.Id;
            }
            catch
            {
                return -1;
            }
        }
        public static SD_Student_Attachments  Single(long id)
        {
            try
            {
                return db.SD_Student_Attachments.Single(x => x.Id == id);
            }
            catch
            {
                return null;
            }
        }


        public static IEnumerable<SD_Student_Attachments> All(Hashtable items)
        {
            SchoolEntities db = new SchoolEntities();

            var query = from Attachment in db.SD_Student_Attachments select Attachment;
            foreach (DictionaryEntry item in items)
            {
                if (item.Key == "Id")
                {
                    long Id;
                    Id= Convert.ToInt64(item.Value); 
                    query = query.Where(c => c.Id  == Id );
                }
                if (item.Key == "Student_Id")
                {
                    long Student_Id = Convert.ToInt64(item.Value);
                    query = query.Where(c => c.Student_Id == Student_Id);
                }
                if (item.Key == "AttachFileName")
                {

                    query = query.Where(c => c.AttachFileName == item.Value);
                }
                if (item.Key == "AttachDescription")
                {

                    query = query.Where(c => c.Attach_Description == item.Value);
                    
                }
            }
            return query.ToList();
        }

        public static void Update(SD_Student_Attachments Attachment)
        {

            SD_Student_Attachments c = db.SD_Student_Attachments.Single(x => x.Id == Attachment.Id);
            c.Student_Id = Attachment.Student_Id;
            c.AttachFileName = Attachment.AttachFileName;
            c.Attach_Description = Attachment.Attach_Description;
            db.SaveChanges();

        }

        public static Boolean  Delete(long id)
        {
            try
            {
                db.SD_Student_Attachments.DeleteObject(db.SD_Student_Attachments.Single(x => x.Id == id));
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
        

    }
}

